home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / demograph / states.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  654 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "stdio.h"
  18. #include "gl.h"
  19. #include "math.h"
  20. #include "device.h"
  21. #include "demograph.h"
  22. #include <sys/time.h>
  23.  
  24. /* used to flip map (this can be replaced when ui.c is implemented) */
  25. #define TZMAXINC 0.1
  26. #define TXYMAXINC 0.1
  27. #define RMAXINC 50.0
  28.  
  29. /* number of frames between status updates */
  30. #define WRITE_INC 7
  31.  
  32. extern MACHREC machrec;
  33. extern WINREC winrec;
  34. extern char *statesbuf;
  35. extern char usage[];
  36. extern int drawtype;
  37.  
  38. float sry = 200.0;
  39. float srx = -300.0;
  40. float srz = 1.0;
  41. float stz = -3.0;
  42. float sty = -0.25;
  43. float stx = -0.1;
  44.  
  45. float time0, time1;
  46. static float mytime;
  47. int frames = 0;
  48.  
  49. Matrix mprojection, mviewing;
  50.  
  51. void getstates (argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.     FILE *file;
  56.     long ivar;
  57.     int *intptr;
  58.     int found = FALSE;
  59.     char *statesflag;
  60.     char filename[255];
  61.  
  62.     FILE *openfile(char *filename, char *readwrite);
  63.  
  64.     statesflag = "-s";
  65.  
  66.     /* look for states file in parameter list */
  67.     for (ivar = 1; ivar < argc; ivar++) {
  68.     if (strcmp(statesflag,argv[ivar]) == 0) {
  69.         ivar++;
  70.         found = TRUE;
  71.         break;
  72.     }
  73.     }
  74.  
  75.     /* check for errors - no data file, no states file */ 
  76.     if (argc < 2 || found && (argc < 4 ||ivar == argc)) {
  77.     fprintf(stderr,"%s",usage);
  78.     exit(1);
  79.     }
  80.     
  81.     /* if not found try default file else try given file */ 
  82.     if (!found)
  83.     strcpy(filename,"states.bin");
  84.     else
  85.     strncpy(filename,argv[ivar],255);
  86.  
  87.     file = openfile(filename,"r");
  88.  
  89.     /* go to end of file, get filesize, then rewind to beginning of file */
  90.     fseek(file,0,2);
  91.     ivar = ftell(file);
  92.  
  93.     if (ivar == 0) {
  94.     fprintf(stderr,"Nothing in states file: %s\n",filename);
  95.     exit(1);
  96.     }
  97.  
  98.     fseek(file,0,0);
  99.  
  100.     /* make room for states */
  101.     statesbuf = (char *)malloc(ivar);
  102.  
  103.     /* blast in states */
  104.     fread(statesbuf,sizeof(char),ivar,file);
  105.  
  106.     intptr = (int *)statesbuf;
  107.  
  108.     if (*intptr != CURVERSION) {
  109.     fprintf(stderr,"Error: states file %s version is not current\n",
  110.         filename);
  111.     exit(1);
  112.     }
  113. }
  114.     
  115. typedef float VTX[3];
  116.  
  117. /* funky states format is as follows:
  118.     Each state consists of:
  119.     nvolumes    number of volumes for this state
  120.     npolys        number of polygons in firs volume
  121.     nvertices   number of vertices in top
  122.     xyznormal
  123.     vtx vtx vtx vtx vtx ...
  124.     nvertices   number of vertices in side 1
  125.     xyznormal
  126.     vtx vtx vtx vtx
  127.     nvertices   number of vertices in side 2
  128.     xyznormal
  129.     vtx vtx vtx vtx
  130.     nvertices   number of vertices in side 
  131.     xyznormal
  132.     vtx vtx vtx vtx
  133.     ...
  134.     npolys        number of polygons in second volume
  135.     nvertices   number of vertices in top
  136.     xyznormal
  137.     vtx vtx vtx vtx vtx ...
  138.     nvertices   number of vertices in side 1
  139.     xyznormal
  140.     vtx vtx vtx vtx
  141.     nvertices   number of vertices in side 2
  142.     xyznormal
  143.     vtx vtx vtx vtx
  144.     nvertices   number of vertices in side 3
  145.     xyznormal
  146.     vtx vtx vtx vtx
  147.     nvertices   number of vertices in side
  148.     xyznormal
  149.     vtx vtx vtx vtx
  150.     
  151.     ...
  152. Next state goes here
  153. */
  154. #define BIG_COLOROFFSET    131
  155. #define COLOROFFSET    1
  156.  
  157. draw_line_map(dataptr)
  158. register float *dataptr;
  159. {
  160.     register int    p;
  161.     register int    nv;
  162.     register int    nvolumes;
  163.     register int    npolys;
  164.     register int    nvertices;
  165.     register int   *intptr;
  166.     register VTX   *vtxptr;
  167.  
  168.     short           r, g, b;
  169.  
  170.     /* we draw the tops here - maybe */
  171.     if (drawtype) {
  172.     zbuffer(TRUE);
  173.     }    
  174.  
  175.     if (winrec.fps) {
  176.     clock_sec(&time0);
  177.     }
  178.  
  179.     mmode(MPROJECTION);
  180.     getmatrix(mprojection);
  181.     mmode(MVIEWING);
  182.     getmatrix(mviewing);
  183.  
  184.     pushviewport();
  185.     viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
  186.     perspective(300, 1.0, 0.1, 5.0);
  187.  
  188.     /* temporary hardwired scale and rot */
  189.     /* pushmatrix(); */
  190.     translate(stx,sty,stz);
  191.     rotate((int)sry,'y');
  192.     rotate((int)srx,'x');
  193.  
  194.     /* inly needed for drawing states */
  195.     if (drawtype) {
  196.     zclear();
  197.     }
  198.  
  199.     setcolor(0,0,0);
  200.     clear();
  201.  
  202.     intptr = (int *) statesbuf;
  203.     /* bypass version number */
  204.     intptr++;
  205.  
  206.     /* 48 states - colors could use some work */
  207.     for (r = 127; r < 256; r += 64) {    /* 3 times */
  208.     for (g = 255; g > 0; g -= 64) {   /* 4 times */
  209.          for (b = 255; b > 0; b -= 64) {    /* 4 times */
  210.  
  211.         /* scale for this state */
  212.         pushmatrix();
  213.         scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
  214.         /* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
  215.         setcolor(r, g, b);
  216.  
  217.             /* draw each volume */
  218.             nvolumes = *intptr++;
  219.             for (nv = 0; nv < nvolumes; nv++) {
  220.  
  221.             /* get number of polygons that make up volume */
  222.             npolys = *intptr++;
  223.  
  224.             /* get number of vertices for the top */
  225.             nvertices = *intptr++;
  226.  
  227.             /* skip over the normal for now */
  228.             vtxptr = (VTX *)intptr;
  229.             vtxptr++;
  230.  
  231.             /* skip over the top polygon if no zbuffer */
  232.             if (drawtype) {
  233.                 concave(TRUE);
  234.                 polf(nvertices,vtxptr);
  235.                 concave(FALSE);
  236.             }
  237.             vtxptr += nvertices;
  238.             intptr = (int *) vtxptr;
  239.  
  240.             /* draw the side polygons - there are always 4
  241.              * sides 
  242.              */
  243.             for (p = 1; p < npolys; p++) {
  244.  
  245.                 nvertices = *intptr++;
  246.  
  247.                 /* skip over the normal for now */
  248.                 vtxptr = (VTX *)intptr;
  249.                 vtxptr++;
  250.  
  251.                 poly(nvertices,vtxptr);
  252.                 vtxptr += nvertices;
  253.  
  254.                 intptr = (int *) vtxptr;
  255.             }
  256.             }
  257.         dataptr++;
  258.         popmatrix();
  259.         }
  260.     }
  261.     }
  262.     /* popmatrix(); */
  263.     popviewport();
  264.  
  265.     mmode(MPROJECTION);
  266.     loadmatrix(mprojection);
  267.     mmode(MVIEWING);
  268.     loadmatrix(mviewing);
  269.  
  270.     /* only if we draw the tops */
  271.     if (drawtype) {
  272.     zbuffer(FALSE);
  273.     }
  274.  
  275.     if (winrec.fps) {
  276.     clock_sec(&time1);
  277.     mytime += (time1 - time0);
  278.     frames++;
  279.     if (frames == WRITE_INC) {
  280.         winrec.lasttime = frames/mytime;
  281.         writefps(winrec.lasttime);
  282.         frames = 0;
  283.         mytime = 0;
  284.     }
  285.     }
  286. }
  287.  
  288.  
  289. draw_solid_map(dataptr)
  290. register float *dataptr;
  291. {
  292.     register int    p;
  293.     register int    nv;
  294.     register int    nvolumes;
  295.     register int    npolys;
  296.     register int    nvertices;
  297.     register int   *intptr;
  298.     register float  x, y;
  299.     register VTX   *vtxptr;
  300.  
  301.     short           r, g, b;
  302.  
  303.     zbuffer(TRUE);
  304.     backface(TRUE);
  305.  
  306.     if (winrec.fps) {
  307.     clock_sec(&time0);
  308.     }
  309.     mmode(MPROJECTION);
  310.     getmatrix(mprojection);
  311.     mmode(MVIEWING);
  312.     getmatrix(mviewing);
  313.  
  314.     pushviewport();
  315.     viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
  316.     perspective(300, 1.0, 0.1, 5.0);
  317.  
  318.     /* temporary hardwired scale and rot */
  319.     /* pushmatrix(); */
  320.     translate(stx, sty, stz);
  321.     rotate((int) sry, 'y');
  322.     rotate((int) srx, 'x');
  323.  
  324.     /* inly needed for drawing states */
  325.     zclear();
  326.  
  327.     setcolor(0, 0, 0);
  328.     clear();
  329.  
  330.     intptr = (int *) statesbuf;
  331.     /* bypass version number */
  332.     intptr++;
  333.  
  334.     /* 48 states - colors could use some work */
  335.     for (r = 127; r < 256; r += 64) {    /* 3 times */
  336.     for (g = 255; g > 0; g -= 64) {    /* 4 times */
  337.         for (b = 255; b > 0; b -= 64) {    /* 4 times */
  338.         /* fprintf(stderr,"r g b = %d %d %d\n",r,g,b); */
  339.  
  340.         /* scale for this state */
  341.         pushmatrix();
  342.         scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
  343.         /* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
  344.  
  345.         /* draw each volume */
  346.         nvolumes = *intptr++;
  347.         for (nv = 0; nv < nvolumes; nv++) {
  348.  
  349.             setcolor(r, g, b);
  350.  
  351.             /* get number of polygons that make up volume */
  352.             npolys = *intptr++;
  353.  
  354.             /* get number of vertices for the top */
  355.             nvertices = *intptr++;
  356.  
  357.             /* skip over the normal for now */
  358.             vtxptr = (VTX *) intptr;
  359.             vtxptr++;
  360.  
  361.             /** intptr = (int *) (vtxptr + 1); **/
  362.  
  363.             /* assume we always draw the top polygon */
  364.             /** vtxptr = (VTX *) intptr; **/
  365.             concave(TRUE);
  366.             polf(nvertices,vtxptr);
  367.             concave(FALSE);
  368.  
  369.             /* outline the top on eight bitplane machine */
  370.             if (!machrec.RGB) {
  371.             color(0);
  372.             poly(nvertices,vtxptr);
  373.             setcolor(r,g,b);
  374.             }
  375.             vtxptr += nvertices;
  376.  
  377.             intptr = (int *) vtxptr;
  378.  
  379.             /* draw the side polygons - there are always 4 sides */
  380.             for (p = 1; p < npolys; p++) {
  381.  
  382.             nvertices = *intptr++;
  383.  
  384.             /* draw shaded colors */
  385.             x = *((float *) intptr);
  386.             y = *(((float *) intptr) + 1);
  387.             if (x >= 0.0 && y <= 0.0) {
  388.                 setcolor(
  389.                 (int)(r*0.5),(int)(g*0.5),(int)(b*0.5));
  390.             } else if (x <= 0.0 && y >= 0.0) {
  391.                 setcolor(
  392.                 (int)(r*0.35),(int)(g*0.35),(int)(b*0.35));
  393.             } else if (x >= 0.0 && y >= 0.0) {
  394.                 setcolor(
  395.                 (int)(r*0.2),(int)(g*0.2),(int)(b*0.2));
  396.             } else {
  397.                 setcolor(
  398.                 (int)(r*0.7),(int)(g*0.7),(int)(b*0.7));
  399.             }
  400.  
  401.             /* skip over the normal for now */
  402.             vtxptr = (VTX *) intptr;
  403.             vtxptr++;
  404.  
  405.             /* draw the side */
  406.             polf(nvertices,vtxptr);
  407.             vtxptr += nvertices;
  408.  
  409.             intptr = (int *) vtxptr;
  410.             }
  411.         }
  412.         dataptr++;
  413.         popmatrix();
  414.         }
  415.     }
  416.     }
  417.     /* popmatrix(); */
  418.     popviewport();
  419.  
  420.     mmode(MPROJECTION);
  421.     loadmatrix(mprojection);
  422.     mmode(MVIEWING);
  423.     loadmatrix(mviewing);
  424.  
  425.     zbuffer(FALSE);
  426.     backface(FALSE);
  427.  
  428.     if (winrec.fps) {
  429.     clock_sec(&time1);
  430.     mytime += (time1 - time0);
  431.     frames++;
  432.     if (frames == WRITE_INC) {
  433.         winrec.lasttime = frames/mytime;
  434.         writefps(winrec.lasttime);
  435.         frames = 0;
  436.         mytime = 0;
  437.     }
  438.     }
  439. }
  440.  
  441.  
  442. draw_lighted_map(dataptr)
  443. register float *dataptr;
  444. {
  445.     register int    p;
  446.     register int    nv;
  447.     register int    i;
  448.     register int    nvolumes;
  449.     register int    npolys;
  450.     register int    nvertices;
  451.     register int   *intptr;
  452.     register VTX   *vtxptr;
  453.     register VTX   *normal;
  454.  
  455.     short           r, g, b;
  456.  
  457.     if (winrec.fps) {
  458.     clock_sec(&time0);
  459.     }
  460.  
  461.     shademodel(GOURAUD);
  462.     zbuffer(TRUE);
  463.     backface(TRUE);
  464.  
  465.     mmode(MPROJECTION);
  466.     getmatrix(mprojection);
  467.     mmode(MVIEWING);
  468.     getmatrix(mviewing);
  469.  
  470.     pushviewport();
  471.     viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
  472.     perspective(300, 1.0, 0.1, 5.0);
  473.  
  474.     lmbind(LIGHT0, 1);
  475.  
  476.     /* temporary hardwired scale and rot */
  477.     /* pushmatrix(); */
  478.     translate(stx, sty, stz);
  479.     rotate((int) sry, 'y');
  480.     rotate((int) srx, 'x');
  481.  
  482.     lmcolor(LMC_AD);
  483.  
  484.     /* inly needed for drawing states */
  485.     zclear();
  486.  
  487.     setcolor(0, 0, 0);
  488.     clear();
  489.  
  490.     intptr = (int *) statesbuf;
  491.     /* bypass version number */
  492.     intptr++;
  493.  
  494.     /* 48 states - colors could use some work */
  495.     for (r = 127; r < 256; r += 64) {    /* 3 times */
  496.     for (g = 255; g > 0; g -= 64) {    /* 4 times */
  497.         for (b = 255; b > 0; b -= 64) {    /* 4 times */
  498.  
  499.         RGBcolor(r, g, b);
  500.  
  501.         /* scale for this state */
  502.         pushmatrix();
  503.         scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
  504.  
  505.         /* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
  506.  
  507.         /* draw each volume */
  508.         nvolumes = *intptr++;
  509.         for (nv = 0; nv < nvolumes; nv++) {
  510.  
  511.             /* get number of polygons that make up volume */
  512.             npolys = *intptr++;
  513.  
  514.             /* get number of vertices for the top */
  515.             nvertices = *intptr++;
  516.  
  517.             /* get the normal */
  518.             vtxptr = (VTX *) intptr;
  519.             normal = vtxptr++;
  520.  
  521.             /* assume we always draw the top polygon */
  522.             concave(TRUE);
  523.             bgnpolygon();
  524.             for (i = nvertices; i > 0; i--) {
  525.             n3f(normal);
  526.             v3f(vtxptr++);
  527.             }
  528.             endpolygon();
  529.             concave(FALSE);
  530.  
  531.             intptr = (int *) vtxptr;
  532.  
  533.             /* draw the side polygons - there are always 4 sides */
  534.             for (p = 1; p < npolys; p++) {
  535.  
  536.             nvertices = *intptr++;
  537.  
  538.             /* get the normal */
  539.             vtxptr = (VTX *) intptr;
  540.             normal = vtxptr++;
  541.  
  542.             /* draw this side */
  543.             bgnpolygon();
  544.                 for (i = 0; i < nvertices; i++) { 
  545.                 n3f(normal); 
  546.                 v3f(vtxptr++);
  547.                 }
  548.             endpolygon(); 
  549.  
  550.             intptr = (int *) vtxptr;
  551.             }
  552.         }
  553.         dataptr++;
  554.         popmatrix();
  555.         }
  556.     }
  557.     }
  558.     /* popmatrix(); */
  559.     popviewport();
  560.  
  561.     mmode(MPROJECTION);
  562.     loadmatrix(mprojection);
  563.     mmode(MVIEWING);
  564.     loadmatrix(mviewing);
  565.  
  566.     lmcolor(LMC_COLOR);
  567.  
  568.     zbuffer(FALSE);
  569.     backface(FALSE);
  570.     shademodel(FLAT);
  571.  
  572.     if (winrec.fps) {
  573.     clock_sec(&time1);
  574.     mytime += (time1 - time0);
  575.     frames++;
  576.     if (frames == WRITE_INC) {
  577.         winrec.lasttime = frames/mytime;
  578.         writefps(winrec.lasttime);
  579.         frames = 0;
  580.         mytime = 0;
  581.     }
  582.     }
  583. }
  584.  
  585.  
  586. flipmap(mode)
  587. int mode;
  588. {
  589.     float tzscaler, txyscaler, rscaler;
  590.     int x, y;
  591.     int cx, cy;
  592.     int orgx, orgy;
  593.     int dev = 0;
  594.     short val = 0;
  595.  
  596.  
  597.     cx = (int) ((winrec.mapminx + winrec.mapmaxx) / 2.0 + 0.5);
  598.     cy = (int) ((winrec.mapminy + winrec.mapmaxy) / 2.0 + 0.5);
  599.     /* fprintf(stderr,"mminx%d mmaxx%d mminy%d mmaxy%d cy%d cx%d\n",
  600.     winrec.mapminx,winrec.mapmaxx,winrec.mapminy,winrec.mapmaxy,cy,cx);
  601.     */
  602.  
  603.     orgx = winrec.orgx;
  604.     orgy = winrec.orgy;
  605.     /* fprintf(stderr,"orgx%d orgy%d\n",orgx,orgy); */
  606.  
  607.     tzscaler = TZMAXINC / cx;
  608.     txyscaler = TXYMAXINC / cx;
  609.     rscaler = RMAXINC / cx;
  610.  
  611.     do {
  612.     x = getvaluator(MOUSEX) - orgx;
  613.     y = getvaluator(MOUSEY) - orgy;
  614.     /* fprintf(stderr,"x%d y%d\n",x,y); */
  615.  
  616.     if (getview(x,y) == MAPPORT) {
  617.         switch (mode) {
  618.         case SPIN:
  619.             sry += (x - cx) * rscaler;
  620.             srx += -((y - cy) * rscaler);
  621.             break;
  622.         case TRANSXY:
  623.             stx += (x - cx) * txyscaler;
  624.             sty += (y - cy) * txyscaler;
  625.         case TRANSZ:
  626.             stz += (x - cx) * tzscaler;
  627.         }
  628.         drawaction();
  629.     }
  630.     } while (!(qtest() && 
  631.     ((dev = qread(&val)) == LEFTMOUSE && mode == TRANSXY) || 
  632.     (dev == MIDDLEMOUSE && mode == SPIN) && 
  633.     val == UP));
  634.     qreset();
  635. }
  636.  
  637.  
  638. clock_sec(s)
  639. float *s;
  640. {
  641.  
  642. float  tmp1,tmp2;
  643. long   err;
  644. struct timeval tp;
  645. struct timezone tzp;
  646.  
  647.     err = gettimeofday(&tp,&tzp);
  648.  
  649.     tmp1 =  (float)(tp.tv_usec)/1000000.0;
  650.     tmp2 =  tp.tv_sec%10000;
  651.     *s = tmp1+tmp2;
  652.     return;
  653. }
  654.